#nullable enable
using Furion.DependencyInjection;
using Furion.Shapeless;

namespace Application.Web.Core;

/// <summary>
/// 全局应用程序上下文单例
/// <para>提供线程安全的全局对象存储和访问机制</para>
/// </summary>
/// <remarks>
/// <para><b>功能特性：</b></para>
/// <list type="bullet">
/// <item><description>线程安全：使用锁机制确保多线程环境下的安全访问</description></item>
/// <item><description>延迟初始化：首次访问时自动创建实例</description></item>
/// <item><description>全局共享：应用程序生命周期内保持单例</description></item>
/// <item><description>动态对象：使用Clay动态对象支持灵活的数据结构</description></item>
/// </list>
/// 
/// <para><b>使用示例：</b></para>
/// <code>
/// // 存储全局配置信息
/// AppContext.GLOBALVAR.AppSettings = new { Database = "TestDB", Timeout = 30 };
/// 
/// // 存储用户会话信息
/// AppContext.GLOBALVAR.CurrentUser = new { Id = 1, Name = "Admin", Roles = new[] { "Admin", "User" } };
/// 
/// // 存储缓存数据
/// AppContext.GLOBALVAR.Cache = new { Users = userList, Permissions = permissionList };
/// 
/// // 读取全局数据
/// var settings = AppContext.GLOBALVAR.AppSettings;
/// var currentUser = AppContext.GLOBALVAR.CurrentUser;
/// var cache = AppContext.GLOBALVAR.Cache;
/// 
/// // 动态属性访问
/// string databaseName = AppContext.GLOBALVAR.AppSettings.Database;
/// int userId = AppContext.GLOBALVAR.CurrentUser.Id;
/// 
/// // 多线程安全使用示例
/// Parallel.For(0, 10, i =>
/// {
///     // 线程安全的全局数据访问
///     AppContext.GLOBALVAR.ThreadSafeCounter = i;
///     
///     // 每个线程存储独立数据
///     AppContext.GLOBALVAR[$"ThreadData_{i}"] = new { ThreadId = i, Timestamp = DateTime.Now };
///     
///     // 安全的并发读取
///     var counter = AppContext.GLOBALVAR.ThreadSafeCounter;
///     var threadData = AppContext.GLOBALVAR[$"ThreadData_{i}"];
/// });
/// 
/// // 动态对象灵活数据结构示例
/// // 1. 动态添加嵌套对象
/// AppContext.GLOBALVAR.Configuration = new Clay();
/// AppContext.GLOBALVAR.Configuration.Database = new { ConnectionString = "Server=localhost", Timeout = 30 };
/// AppContext.GLOBALVAR.Configuration.Cache = new { Expiration = 3600, Size = "100MB" };
/// 
/// // 2. 动态数组操作
/// AppContext.GLOBALVAR.RecentActivities = new ClayArray();
/// AppContext.GLOBALVAR.RecentActivities.Add(new { Action = "Login", User = "Admin", Time = DateTime.Now });
/// AppContext.GLOBALVAR.RecentActivities.Add(new { Action = "Logout", User = "User1", Time = DateTime.Now.AddMinutes(-5) });
/// 
/// // 3. 动态方法调用
/// AppContext.GLOBALVAR.GetUserInfo = new Func<int, object>(userId => 
/// {
///     return new { Id = userId, Name = $"User{userId}", LastLogin = DateTime.Now };
/// });
/// 
/// // 4. 条件动态属性
/// AppContext.GLOBALVAR.IsDebugMode = true;
/// if (AppContext.GLOBALVAR.IsDebugMode)
/// {
///     AppContext.GLOBALVAR.DebugInfo = new { LogLevel = "Verbose", TraceEnabled = true };
/// }
/// 
/// // 5. 动态合并对象
/// var userPrefs = new { Theme = "Dark", Language = "zh-CN" };
/// AppContext.GLOBALVAR.UserPreferences = AppContext.GLOBALVAR.UserPreferences.Merge(userPrefs);
/// 
/// // 6. 动态索引器访问
/// for (int i = 0; i < AppContext.GLOBALVAR.RecentActivities.Count; i++)
/// {
///     var activity = AppContext.GLOBALVAR.RecentActivities[i];
///     Console.WriteLine($"Activity: {activity.Action} by {activity.User}");
/// }
/// </code>
/// 
/// <para><b>最佳实践：</b></para>
/// <list type="bullet">
/// <item><description>适用于存储应用程序级别的全局数据</description></item>
/// <item><description>避免存储大量数据，主要用于轻量级配置和状态管理</description></item>
/// <item><description>在多线程环境中安全使用</description></item>
/// <item><description>支持动态对象操作，无需预定义类型</description></item>
/// </list>
/// </remarks>
public class AppContext : ISingleton
{
    private static Clay _globalVarInstance = new Clay();
    private static readonly object lock_obj = new object();

    public static Clay GLOBALVAR
    {
        get
        {
            lock (lock_obj)
            {
                return _globalVarInstance ??= new Clay();
            }
        }
    }
}